﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StuApart
{
    public partial class Student : Form
    {
        public Student()
        {
            InitializeComponent();
        }

        private void studentBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.studentBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.stuApartDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“stuApartDataSet.Student”中。您可以根据需要移动或移除它。
            this.studentTableAdapter.Fill(this.stuApartDataSet.Student);
            // TODO: 这行代码将数据加载到表“stuApartDataSet.Student”中。您可以根据需要移动或移除它。
            this.studentTableAdapter.Fill(this.stuApartDataSet.Student);

        }

        private void studentBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
            this.Validate();
            this.studentBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.stuApartDataSet);
            MessageBox.Show("保存成功", "提示");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap image = new Bitmap(openFileDialog1.FileName);
                imagePictureBox.Image = image;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            imagePictureBox.Image = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conditionStr = "";
            if (textBox1.Text.Trim() != "")
            {
                if (conditionStr == "")
                    conditionStr = "Name LIKE'" + textBox1.Text.Trim() + "%'";
                else
                    conditionStr += "AND Name LIKE'" + textBox1.Text.Trim() + "%'";
            }
            if (textBox2.Text.Trim() != "")
                conditionStr = "No LIKE'" + textBox2.Text.Trim() + "%'";
            if (comboBox1.Text.Trim() != "")
            {
                string str = comboBox1.Text.Trim();
                string[] xbStr = str.Split(' ');
                if (xbStr[0] != "")
                {
                    if (conditionStr != "")
                        conditionStr += "AND Sex='" + xbStr[0] + "'";
                    else
                        conditionStr = "Sex='" + xbStr[0] + "'";
                }
            }
            if (ClasscomboBox.Text.Trim() != "")
            {
                string str = ClasscomboBox.Text.Trim();
                string[] bjStr = str.Split(' ');
                if (bjStr[0] != "")
                {
                    if (conditionStr != "")
                        conditionStr += "AND Class='" + bjStr[0] + "'";
                    else
                        conditionStr = "Class='" + bjStr[0] + "'";
                }
            }
            studentBindingSource.Filter = conditionStr;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (studentDataGridView.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先单击最左边的空白处，选择要删除的行！");
            }
            else
            {
                if (MessageBox.Show("你确定要删除吗？", "小心", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    for (int i = studentDataGridView.SelectedRows.Count - 1; i >= 0; i--)
                    { studentBindingSource.RemoveAt(studentDataGridView.SelectedRows[i].Index); }

                }
            }
        }

        
    }
}
